//
//  AddConsumeVController.m
//  Travel assistant
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "AddConsumeVController.h"
#import "ConsumeTypeView.h"
#import "DateViewController.h"

@interface AddConsumeVController ()
@property (nonatomic, assign) NSInteger markIndex;
@property (nonatomic, strong) ConsumptioModel *consumptioModel;

@end

@implementation AddConsumeVController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setViews];
    self.addConsumeLable.userInteractionEnabled = YES;
    self.dateLable.userInteractionEnabled = YES;
    self.addNoteLable.userInteractionEnabled = YES;
    [self.addConsumeLable addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addConsumeLableAction:)]];
    [self.dateLable addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dateLableAction:)]];
    
    [self.addNoteLable addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addNoteLableAction:)]];
}
- (void)viewWillAppear:(BOOL)animated{
    
}
- (void)setViews{
    self.scrollerView.contentSize = CGSizeMake(self.view.bounds.size.width, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    /**添加消费类型*/
    for (int i = 0; i < self.consumeArray.count; i++) {
        NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ConsumeTypeView" owner:nil options:nil];
        ConsumeTypeView *consumeTV = [nibViews objectAtIndex:0];
        consumeTV.consumeImV.image = [UIImage imageNamed:self.consumeArray[i][@"imageName"]];
        NSLog(@"%@",self.consumeArray[i][@"imageName"]);
        consumeTV.consuneLable.text = self.consumeArray[i][@"name"];
        CGFloat x = Screen_W * 0.2 * i;
        CGFloat width = Screen_W * 0.2 ;
        CGFloat height = self.scrollerView.bounds.size.height;
        consumeTV.frame = CGRectMake(x, 0, width, height);
        [self.scrollerView addSubview:consumeTV];
        /**weiView添加手势*/
        consumeTV.tag = 10000 + i;
        [consumeTV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapConsumeTVAction:)]];
        //consumeTV.scale = 0.0;
    }
    
    
    
    
}
- (void)tapConsumeTVAction:(UIGestureRecognizer *)sender{
    self.type = (long)sender.view.tag - 10000;
}
- (IBAction)saveButtonAction:(UIButton *)sender {
    NSLog(@"dsfgsdkfdlsgfndsgfsd");
    self.consumptioModel = [[ConsumptioModel alloc] initWithConsumType:[NSString stringWithFormat:@"%ld",self.type] ConsumAmount:self.addConsumeLable.text consumCurrency:@"人民币" consumTime:self.dateLable.text consumNote:self.addNoteLable.text ];
    /**离线缓存*/
    [self.consumptioModel saveEventually:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"离线缓存成功......");
#pragma mark ---继续对其他（子类化对象）进行存储-------
            
            
            
            
            
        }
    }];
}

- (NSArray *)consumeArray{
    if (!_consumeArray) {
        _consumeArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AddConsume" ofType:@"plist"]];
    }
    return _consumeArray;
}
- (void)addNoteLableAction:(UIGestureRecognizer *)sender{
    [self addAlertViewcontrollerWithTitle:@"备注" placeholder:@"又花钱了..." subLable:(UILabel *)sender.view];
}
- (void)addConsumeLableAction:(UIGestureRecognizer *)sender {
    [self addAlertViewcontrollerWithTitle:@"消费金额" placeholder:@"请输入消费金额..." subLable:(UILabel *)sender.view];
}
- (void)addAlertViewcontrollerWithTitle:(NSString *)title placeholder:(NSString *)placeholder subLable:(UILabel *)lable{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = placeholder;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        lable.text =((UITextField *)alertController.textFields.firstObject).text;
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)dateLableAction:(UIGestureRecognizer *)sender{
    DateViewController *dateVC = [[DateViewController alloc] init];
    UINavigationController *NaVC = [[UINavigationController alloc] initWithRootViewController:dateVC];
    [self presentViewController:NaVC animated:YES completion:^{
        __weak typeof(self) __self = self;
        dateVC.passDateBlock = ^(NSDate *date){
            NSDate *jjDate = [[DateHelper sharedDateHelper] dayDateWithDate:date AddHours:YES];
            __self.dateLable.text = [__self stringFromDate:jjDate ];
        };
    }];
}
/**Data 在转变为字符串*/
- (NSString *)stringFromDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss zzz"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    NSString *string = [destDateString substringToIndex:10];
    return string;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
